package com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.service.business.msg.convert;

import cn.hutool.json.JSONObject;
import com.yonyou.iuap.ipaas.connector.sdk.exception.BusinessModuleException;
import com.yonyou.iuap.ipaas.connector.sdk.exception.CommonErrorTypeDefinition;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.business.msg.BaseToDo;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.business.msg.extend.*;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.SdwcoatencentConnection;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.service.core.ParamConvertService;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.utils.LogUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class WarningParamConvertService implements ParamConvertService<BaseToDo, BipWarning> {
    @Override
    public List<BaseToDo> convertForBatchSave(List<BipWarning> list, SdwcoatencentConnection connection) {
        String corpid = connection.getCorpid();
        Long agentid = connection.getAgentid();
        List<BaseToDo> baseTodos = new ArrayList<>();
        String authInterface = connection.getAuthInterface();
        Iterator var7 = list.iterator();
        while(var7.hasNext()) {
            BipWarning warning = (BipWarning)var7.next();
            StringBuilder authUrlBuilder = new StringBuilder();
            authUrlBuilder.append(authInterface);
            List<String> executorIdsList = warning.getExecutorIdsList();
            if (executorIdsList.isEmpty())
                continue;
            Boolean syncFlag = warning.getSyncFlag();
            if (!syncFlag.booleanValue())
                continue;
            String executorIds = executorIdsList.stream().collect(Collectors.joining("|"));
            warning.setTouser(executorIds);
            warning.setAgentid(agentid);
            String webUrl = (warning.getWebUrl() == null) ? "" : warning.getWebUrl();
            String mUrl = (warning.getMUrl() == null) ? "" : warning.getMUrl();
            authUrlBuilder.append("/"+warning.getTenantId()+"/"+executorIds);
            try {
                //地址处理
                String state = URLEncoder.encode("bizUrl=" + URLEncoder.encode("webUrl=" + URLEncoder.encode(webUrl, "UTF-8") + "&mUrl=" + URLEncoder.encode(mUrl, "UTF-8"), "UTF-8"), "UTF-8");
                StringBuilder urlBuilder = new StringBuilder();
                urlBuilder.append(connection.getAuthHost()).append("/connect/oauth2/authorize").append("?appid=").append(corpid).append("&redirect_uri=").append(URLEncoder.encode(authUrlBuilder.toString(), "UTF-8")).append("&response_type=code&scope=snsapi_base&state=").append(state).append("&agentid=").append(agentid).append("#wechat_redirect");
                warning.setMsgtype("textcard");
                Textcard card = new Textcard();
                card.setTitle(warning.getMsgTitle());
                card.setDescription(warning.getMsgContent());
                card.setUrl(urlBuilder.toString());
                warning.setTextcard(card);
                baseTodos.add(warning);
            } catch (UnsupportedEncodingException e) {
                LogUtils.printStacktrace(e);
                throw BusinessModuleException.simpleException("999", "build warning error,errorMsg:" + e.getMessage(), CommonErrorTypeDefinition.CONNECTIVITY);
            }
        }
        return baseTodos;

    }

    @Override
    public List<BaseToDo> convertForBatchDelete(List<BipWarning> list, SdwcoatencentConnection connection) {
        return null;
    }

    @Override
    public BaseToDo convertForSave(BipWarning objet, SdwcoatencentConnection connection) {
        return null;
    }

    @Override
    public BaseToDo convertForDelete(BipWarning objet, SdwcoatencentConnection connection) {
        return null;
    }
}
